package com.softer.ztb.service.impl;

import com.softer.ztb.common.entity.*;
import com.softer.ztb.common.enums.MessageEnum;
import com.softer.ztb.common.enums.ShiroResultEnum;
import com.softer.ztb.entity.SysRole;
import com.softer.ztb.mapper.RoleMapper;
import com.softer.ztb.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;


    @Override
    public ShiroCommonResult insert(SysRole role) {
        int i = roleMapper.insert(role);
        if(i>0){
            return new ShiroCommonResult(ShiroResultEnum.SUCCESS.getCode(), MessageEnum.SUCCESS.getMsg());
        }
        return new ShiroCommonResult(ShiroResultEnum.FAIL.getCode(), MessageEnum.FAIL.getMsg());
    }

    @Override
    public ShiroCommonResult update(SysRole role) {
        int i = roleMapper.insert(role);
        if(i>0){
            return new ShiroCommonResult(ShiroResultEnum.SUCCESS.getCode(), MessageEnum.SUCCESS.getMsg());
        }
        return new ShiroCommonResult(ShiroResultEnum.FAIL.getCode(), MessageEnum.FAIL.getMsg());
    }
    @Override
    public ShiroCommonResult delete(String id) {
        int i = roleMapper.delete(id);
        if(i>0){
            return new ShiroCommonResult(ShiroResultEnum.SUCCESS.getCode(), MessageEnum.SUCCESS.getMsg());
        }
        return new ShiroCommonResult(ShiroResultEnum.FAIL.getCode(), MessageEnum.FAIL.getMsg());
    }

    @Override
    public ManagerPageResult getRoleList(Page page, SysRole role) {
        ManagerPageResult mr = new ManagerPageResult();
        JsonResult jr = new JsonResult();
        page.setQueryBean(role);
        int count = roleMapper.getCount(page);
        if (count > Integer.parseInt(Common.ZERO)) {
            List<SysRole> list = roleMapper.getList(page);
            return mr.setResult(mr, jr, count, list);
        }
        return mr.setResult(mr, jr, Integer.parseInt(Common.ZERO), new ArrayList());
    }
}
